package com.qk.controller;

import com.qk.common.Result;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
        log.info("上传文件:{}",image);

        //1\获取文件名
        //构造一个文件名(不能重复)=>使用 UUID:d1aca08e-7cc3-405c-9408-93954e963a54
        String fileName=image.getOriginalFilename();//获取原始的文件名-------->123.1.2.png
        String nextFileName = fileName.substring(fileName.lastIndexOf("."));//.png
        String newFileName = UUID.randomUUID().toString() + nextFileName;

        String url=aliyunOSSOperator.upload(image.getBytes(), newFileName);

        return Result.success(url);

    }

/*    @PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile file) throws Exception {
        log.info("上传文件:{},{},{}", username, age, file);

        //1\获取文件名
        //构造一个文件名(不能重复)=>使用 UUID:d1aca08e-7cc3-405c-9408-93954e963a54
        String fileName=file.getOriginalFilename();//获取原始的文件名-------->123.1.2.png
        String nextFileName = fileName.substring(fileName.lastIndexOf("."));//.png
        String newFileName = UUID.randomUUID().toString() + nextFileName;

//        2\将文件保存到磁盘中---->指定保存到那个文件中
        file.transferTo(new File("E:\\1黑马教学\\2JavaWeb阶段\\代码\\A-storage\\"+newFileName));


//        //1\将文件保存到磁盘中---->指定保存到那个文件中
//        file.transferTo(new File("E:\\1黑马教学\\2JavaWeb阶段\\代码\\A-storage\\1.txt"));

        return Result.success();

    }*/



}
